package cn.lyjuan.payment.third.base.model.app.domain;

import cn.lyjuan.base.util.RandomUtils;
import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.payment.third.base.common.cst.StateConst;
import cn.lyjuan.payment.third.base.common.entity.app.AppRegEntity;
import cn.lyjuan.payment.third.base.common.mapper.AppRegEntityMapper;
import cn.lyjuan.payment.third.base.model.app.check.AppCheck;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * APP域
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class AppModel {
    @Resource
    private AppRegEntityMapper mapper;

    private AppRegEntity entity;

    /**
     * 新增
     * todo 权限
     *
     * @return
     */
    public boolean add(String name, String province, String city, String district, String adcode, String detail, String tel) {
        entity = new AppRegEntity();
        entity.setName(name);
        entity.setProvince(province);
        entity.setCity(city);
        entity.setDistrict(district);
        entity.setAdcode(adcode);
        entity.setDetail(detail);
        entity.setTel(tel);

        entity.setStatus(StateConst.OK_BL);
        entity.setCreatetime(LocalDateTime.now());

        // 生成md5
        this.entity.setKeyMd5(RandomUtils.uuid());

        return mapper.insert(this.entity) > 0;
    }

    /**
     * 保存信息
     * todo 权限
     *
     * @return
     */
    public boolean save(String name, String province, String city, String district, String adcode, String detail, String tel) {
        AppRegEntity tmp = new AppRegEntity();

        tmp.setId(this.entity.getId());

        if (!StringUtils.isNull(name) && !name.equals(this.entity.getName())) {
            tmp.setName(name);
        }
        // 城市信息可能需要验证
        if (!StringUtils.isNull(province) && !province.equals(this.entity.getProvince())) {
            tmp.setProvince(province);
        }
        if (!StringUtils.isNull(city) && !city.equals(this.entity.getCity())) {
            tmp.setCity(city);
        }
        if (!StringUtils.isNull(district) && !district.equals(this.entity.getDistrict())) {
            tmp.setDistrict(district);
        }
        // todo adcode应该自动获取
        if (!StringUtils.isNull(adcode) && !adcode.equals(this.entity.getAdcode())) {
            tmp.setAdcode(adcode);
        }
        if (!StringUtils.isNull(detail) && !detail.equals(this.entity.getDetail())) {
            tmp.setDetail(detail);
        }
        if (!StringUtils.isNull(tel) && !tel.equals(this.entity.getTel())) {
            tmp.setTel(tel);
        }

        // md5 createtime不能修改
//        String md5 = this.entity.getKeyMd5();
//        this.entity.setKeyMd5(null);
//        LocalDateTime createtime = this.entity.getCreatetime();
//        this.entity.setCreatetime(null);

        int count = this.mapper.updateByPrimaryKeySelective(tmp);
//        this.entity.setKeyMd5(md5);
//        this.entity.setCreatetime(createtime);

        return count > 0;
    }

    public boolean query(Integer id) {
        AppRegEntity tmp = mapper.selectByPrimaryKey(id);
        if (null == tmp) {
            return false;
        }

        this.entity = tmp;
        return true;
    }

    /**
     * 验证签名
     *
     * @param params
     * @return true签名匹配；false不匹配
     */
    public boolean checkSign(Map<String, String> params) {
        return AppCheck.checkSign(params, this.getEntity().getKeyMd5());
    }

    public AppRegEntityMapper getMapper() {
        return mapper;
    }

    public void setMapper(AppRegEntityMapper mapper) {
        this.mapper = mapper;
    }

    public AppRegEntity getEntity() {
        return entity;
    }

    public void setEntity(AppRegEntity entity) {
        this.entity = entity;
    }
}
